import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter_yres/tool/constant.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import './pdd_sub_page.dart';
import 'package:flutter_yres/model/shop/pdd_cate_model.dart';
import 'package:flutter_yres/pages/shop/SearchGoods.dart';

class PddPage extends StatefulWidget {
  PddPage({Key key}) : super(key: key);
  @override
  _PddPageState createState() => _PddPageState();
}

class _PddPageState extends State<PddPage> {
  TabController _tabController;
  List<PddCateModelData> tabs = []; //拼多多顶级分类数组
  
  @override
  Widget build(BuildContext context) {
    return new DefaultTabController(
      length: tabs.length,
      child:  Scaffold(
        appBar:  AppBar(
          backgroundColor: Colors.red,
          title: _buildSearch(),
          bottom:  TabBar(
            isScrollable: true,
            tabs: tabs.map((item) {
              return _tabsBuilder(context, item);
            }).toList(),
            controller: _tabController,
            onTap: (index) {
              print(tabs[index].pdd_id);
            },
          ),
        ),
        body: TabBarView(
          children: tabs.map<Widget>((item) {
            print(item.name);
            // print(item.pdd_cat_id);
            print(item.pdd_id);
              return PddSubPage(pdd_id: item.pdd_id,pdd_cat_id:item.pdd_cat_id);
          }).toList(),
        ),
      ),
    );
  }

  //搜索组件
  _buildSearch() {
    return new Container(
      child: Row(
        children: <Widget>[
          Container(
            color: Colors.red,
            width: ScreenUtil.getInstance().setWidth(500),
            child: new MaterialButton(
              height: ScreenUtil.getInstance().setWidth(60),
              child: Row(
                children: <Widget>[
                  new Icon(Icons.search, color: Colors.grey),
                  new Text('搜索'),
                ],
              ),
              onPressed: () {
                 Navigator.of(context)
                                .push(new MaterialPageRoute(builder: (context) {
                              return new SearchGoodsPage(type:'2');
                            }));
              },
              color: Colors.white,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(20)),
            ),
          ),
        ],
      ),
      width: double.maxFinite,
      margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
      color: Colors.red,
    );
  }

  Widget _tabsBuilder(BuildContext context, item) {
    return Tab(text: item.name);
  }


@override
  void initState() {
    getPDDTopCatList();
    super.initState();
  }


  /*拼多多顶级列表*/
  getPDDTopCatList() async {
    FormData data = new FormData.fromMap({});
    Response<String> response =
        await HttpUtil().post(Api.GetPddTopCatListUrl, data: data);
    Map userMap = json.decode(response.data);
    print(userMap);
    PddCateModelEntity itemEntity = new PddCateModelEntity.fromJson(userMap);
    if (itemEntity.code == 1) {
      if (mounted) {
        setState(() {
          tabs.clear();
          tabs = itemEntity.data;
        });
      }
    }
  }

  
}